import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class NewsContent extends StatefulWidget {
  Map arguments;

  NewsContent({super.key, required this.arguments});

  @override
  State<StatefulWidget> createState() => _NewsContentState();
}

class _NewsContentState extends State<NewsContent> {
  late Map arguments;
  var controller = WebViewController();

  @override
  void initState() {
    super.initState();
    arguments = widget.arguments;
    print(arguments["url"]);
    controller.setJavaScriptMode(JavaScriptMode.unrestricted);
    controller.setNavigationDelegate(
      NavigationDelegate(
        onProgress: (int progress) {
          // Update loading bar.
          print(progress);
        },
        onPageStarted: (String url) {},
        onPageFinished: (String url) {},
        onHttpError: (HttpResponseError error) {},
        onWebResourceError: (WebResourceError error) {},
        onNavigationRequest: (NavigationRequest request) {
          if (request.url.startsWith('https://www.youtube.com/')) {
            return NavigationDecision.prevent;
          }
          return NavigationDecision.navigate;
        },
      ),
    );
    controller.loadRequest(Uri.parse(arguments["url"]));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(arguments["title"]),
      ),
      body: WebViewWidget(controller: controller),
    );
  }
}
